package com.yilin.tms.user.service.impl;

import cn.hutool.core.lang.UUID;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yilin.tms.core.application.PageData;
import com.yilin.tms.core.commons.utils.QueryUtil;
import com.yilin.tms.core.entity.user.account.Account;
import com.yilin.tms.core.entity.user.technical.GoodsConfigure;
import com.yilin.tms.user.mapper.GoodsConfigureMapper;
import com.yilin.tms.user.service.IGoodsConfigureService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.transaction.Transactional;

@Service
public class GoodsConfigureService extends ServiceImpl<GoodsConfigureMapper, GoodsConfigure> implements IGoodsConfigureService {

    @Resource
    GoodsConfigureMapper goodsConfigureMapper;

    @Override
    public GoodsConfigure getGoodsConfigureById(String id) {
        QueryWrapper<GoodsConfigure> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        return goodsConfigureMapper.selectOne(queryWrapper);
    }

    @Override
    public PageData<GoodsConfigure> getGoodsConfigurePage(Account loginUser, GoodsConfigure query, Integer page, Integer limit) {
        QueryWrapper<GoodsConfigure> queryWrapper = new QueryWrapper<>();
        QueryUtil.andQueryWrapper(queryWrapper, query);
        return goodsConfigureMapper.selectPage(new PageData<>(page, limit), queryWrapper);
    }

    @Override
    @Transactional
    public void saveOrUpdateGoodsConfigure(GoodsConfigure goodsConfigure) {
        //变更规则
        if (goodsConfigure.getId() == null) {
            String code = UUID.randomUUID().toString().replace("-", "").substring(0, 4).toUpperCase();
            goodsConfigure.setGoodsCode(code);
            goodsConfigureMapper.insert(goodsConfigure);
        }
        else goodsConfigureMapper.updateById(goodsConfigure);
    }
}
